package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.salelogistics.basic.domain.BasContract;
import com.salelogistics.basic.domain.vo.BasContractVo;
import com.salelogistics.basic.domain.vo.BasLotVo;
import com.salelogistics.basic.utils.CheckParameterUtil;
import com.salelogistics.common.core.utils.StringUtils;
import com.salelogistics.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.domain.BasLot;
import com.salelogistics.basic.service.IBasLotService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 标段Controller
 *
 * @author ruoyi
 * @date 2023-07-09
 */
@RestController
@RequestMapping("lot")
public class BasLotController extends BaseController {
    //日志打印类
    private static final Logger log = LoggerFactory.getLogger(BasSupplierController.class);

    //数据校验工具
    CheckParameterUtil checkParameterUtil = new CheckParameterUtil();

    //随机数
    Random random = new Random();


    @Autowired
    private IBasLotService basLotService;

    public static void main(String[] args) {
        int num=0;
        int n=20;
        int k=0;
        for (int i=1;i<=n;i++){
            for (int j=1;j<i;j++){
                k=j*i;
                System.out.println(j*i);
            }
//            System.out.println(i);
            num+=k;
        }
        System.out.println(num);
    }

    /**
     * 查询标段列表
     */
    @RequiresPermissions("basic:lot:listVo")
    @GetMapping("/listVo")
    public TableDataInfo list(BasLot basLot) {
        List<BasLotVo> listVo=new ArrayList<>();
        startPage();
        List<BasLot> list = basLotService.selectBasLotList(basLot);
        for (int i = 0; i < list.size(); i++) {
            BasLotVo basLotVo = BeanUtil.toBean(list.get(i), BasLotVo.class);
            listVo.add(basLotVo);
        }
        return getDataTable(listVo);
    }

    /**
     * 查询标段列表
     */
    @RequiresPermissions("basic:lot:list")
    @GetMapping("/list")
    public TableDataInfo listVo(BasLot basLot) {
        startPage();
        List<BasLot> list = basLotService.selectBasLotList(basLot);
        return getDataTable(list);
    }

    /**
     * 导出标段列表
     */
    @RequiresPermissions("basic:lot:export")
    @Log(title = "标段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasLot basLot) {
        List<BasLot> list = basLotService.selectBasLotList(basLot);
        ExcelUtil<BasLot> util = new ExcelUtil<BasLot>(BasLot.class);
        util.exportExcel(response, list, "标段数据");
    }

    /**
     * 获取标段详细信息
     */
    @RequiresPermissions("basic:lot:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(basLotService.selectBasLotByCode(id));
    }

    /**
     * 新增标段
     */
    @RequiresPermissions("basic:lot:add")
    @Log(title = "标段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasLot basLot) {
        if (checkParameterUtil.checkParameter(basLot, "basLot")) {
            basLot.setBuilder(SecurityUtils.getUsername());
            basLot.setEditedBy(SecurityUtils.getUsername());
            int randomNumber = random.nextInt(900000) + 100000;
            basLot.setCode("BD"+randomNumber);
            return toAjax(basLotService.insertBasLot(basLot));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 修改标段
     */
    @RequiresPermissions("basic:lot:edit")
    @Log(title = "标段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasLot basLot) {
        if (checkParameterUtil.checkParameter(basLot, "basLot")) {
            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            System.out.println("currentTime"+currentTime);
            basLot.setEditedBy(SecurityUtils.getUsername());
            basLot.setEditedTime(currentTime);
            return toAjax(basLotService.updateBasLot(basLot));
        } else {
            return error("参数异常");
        }

    }

    /**
     * 删除标段
     */
    @RequiresPermissions("basic:lot:remove")
    @Log(title = "标段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable String[] codes) {
        return toAjax(basLotService.deleteBasLotByCodes(codes));
    }
}
